package com.agileai.pm.module.proc.exteral;

import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.pm.cxmodule.ProcProjectTrackManage;

public class ProjTrackImpl extends BaseRestService implements ProjTrack{

	@Override
	public String findTrackingInfos(String projectId,String weekNum) {
		ProcProjectTrackManage procProjectTrackManage=this.lookupService(ProcProjectTrackManage.class);
		DataParam param=new DataParam();
		param.put("projectId",projectId);
		param.put("weekNum",weekNum);
		param.put("pptStartDate", new Date());
		List<DataRow> trackListTotal =procProjectTrackManage.findRecords(param);
		int displayNum = 3;
		if(Integer.parseInt(weekNum)==1){
			displayNum=3;
		}else{
			displayNum=1;
		}
		int startNum=(Integer.parseInt(weekNum)-1)*displayNum;
		int trackListTotalSize = trackListTotal.size();
		param.put("displayNum", displayNum);
		param.put("startNum", startNum);
		JSONObject trackJson = new JSONObject();  
		try {
			List<DataRow> rsList = procProjectTrackManage.findMobileTrackRecords(param);
			int rsListSize = rsList.size();
			JSONArray rsArray = new JSONArray();
			for(DataRow row:rsList){
				JSONObject tempJson = new JSONObject();
				String pptId=row.getString("PPT_ID");  
				String pwiName=row.getString("PWI_NAME"); 
				pwiName=pwiName.split("&nbsp;")[1];
				row.put("PWI_NAME", pwiName);
				
				String pptOneDay=row.stringValue("PPT_ONE_DAY");
				int pptSumDay=row.getInt("PPT_SUM_DAY");
				int pptSurplus=row.getInt("PPT_SURPLUS");
				String pptDescribe=row.getString("PPT_DESCRIBE");
				String pptState=row.getString("PPT_STATE");
				tempJson.put("pwiName", pwiName);
				tempJson.put("pptOneDay", pptOneDay);
				tempJson.put("pptSumDay", pptSumDay);
				tempJson.put("pptSurplus", pptSurplus);
				tempJson.put("pptDescribe", pptDescribe);
				tempJson.put("pptState", pptState);
				tempJson.put("pptId", pptId);
				
				JSONArray jsonTrackArray = new JSONArray();
				List<DataRow> trackList=procProjectTrackManage.findprocTracks(pptId);
				for(DataRow trackRow:trackList){
					JSONObject jsonObject = new JSONObject();
					String ptdId=trackRow.getString("PTD_ID");
					String ptdContentInfo=trackRow.getString("PTD_CONTENT_INFO");
					String ptdOneDay=trackRow.stringValue("PTD_ONE_DAY");
					String ptdSummary=trackRow.stringValue("PTD_SUMMARY");
					jsonObject.put("ptdId", ptdId);
					jsonObject.put("pptId", pptId);
					jsonObject.put("ptdOneDay", ptdOneDay);
					jsonObject.put("ptdContentInfo", ptdContentInfo);
					jsonObject.put("ptdSummary", ptdSummary);
					jsonTrackArray.put(jsonObject);
					
				}
				tempJson.put("trackDetals", jsonTrackArray);
				rsArray.put(tempJson);
				
				row.put("trackList", trackList);
				row.put("trackListSize", trackList.size());
			}
			trackJson.put("trackListTotalSize", trackListTotalSize);
			trackJson.put("rsListSize", rsListSize);
			trackJson.put("trackInfos", rsArray);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		return trackJson.toString();
	}
}